#include "StompedReality/Components/JointComponent.h"
#include "StompedReality/Core/ComponentConstuctionHelper.h"
#include "StompedReality/Core/Graphics/BasicRenderFunctions.h"
#include "StompedReality/Core/TypeSystem/TypeSystem.h"

CreateRegDummy( JointComponent );

StompedReality::components::JointComponent::JointComponent( )
{
}

StompedReality::components::JointComponent::~JointComponent( )
{
}

void StompedReality::components::JointComponent::Draw( )
{
}

void StompedReality::components::JointComponent::OnCreation( )
{
  Component::OnCreation( );
  this->m_Translation = StompedReality::Math::Vector2 ( 0, 0 );
  this->m_Rotation = 0;
  this->m_Depth = 0;
}

void StompedReality::components::JointComponent::OnDestruction( )
{
}

void StompedReality::components::JointComponent::InitializeComponent(
  StompedReality::Core::ComponentConstuctionHelper* constructionHelper )
{
  StompedReality::Graphics::Functions::TranslateRotate* pushFunction =
    new StompedReality::Graphics::Functions::TranslateRotate(
      constructionHelper->GetCurrentJsonValue( ).get( "startTransform", 0 ).asUInt( ), true );

  if ( constructionHelper->IsStaticRenderable( ) )
  {
    this->CopyRenderData ( pushFunction );
  }
  else
  {
    pushFunction->CopyDelegate = StompedReality::CreateDelegate ( this, &JointComponent::CopyRenderData );
  }

  constructionHelper->AddRenderFunction ( pushFunction );

  constructionHelper->AddRenderFunction( new StompedReality::Graphics::Functions::PopMatrix(
                                           constructionHelper->GetCurrentJsonValue( ).get( "endTransform", 0 ).asUInt( )) );
}

bool StompedReality::components::JointComponent::ProcessMessage ( StompedReality::Core::Message* msg )
{
  return true;
}

void StompedReality::components::JointComponent::Update ( )
{
}

void StompedReality::components::JointComponent::CopyRenderData (
  StompedReality::Graphics::Functions::TranslateRotate* fObject )
{
  fObject->Translation = this->m_Translation;
  fObject->Rotation = this->m_Rotation;
}

int StompedReality::components::JointComponentReg::registeType( )
{
  return ( int ) ( StompedReality::type_system::TypeSystem::CreateTypeInfo<JointComponent> ( "JointComponent" )->
                   addTypeField ( StompedReality::type_system::TypeField ( "translation",
                                                                           &JointComponent::m_Translation ) )->
                   addTypeField ( StompedReality::type_system::TypeField ( "rotation",
                                                                           &JointComponent::m_Rotation ) )->
                   addTypeField ( StompedReality::type_system::TypeField ( "depth",
                                                                           &JointComponent::m_Depth ) ) );
}

static int typeReg = StompedReality::components::JointComponentReg::registeType( );
